/**
 * 
 */
package com.tvshows.front.view.refresh;

import java.text.DecimalFormat;
import java.text.NumberFormat;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.ProgressBar;

import com.tvshows.front.ui.refresh.RefreshUI;
import com.tvshows.front.view.labels.FrontLabel;
import com.tvshows.interactions.ObserverBdd;

/**
 * @author tMidy
 */
public class RefreshView
    extends Composite
    implements RefreshUI
{

    /**
     * The progress bar
     */
    private ProgressBar progressBar;

    /**
     * The progress text
     */
    private Label progressLabel;

    /**
     * true if max value is set
     */
    private boolean isSelection;

    /**
     * the former progress of the bar
     */
    private double formerProgress;

    /**
     * No refresh text
     */
    private static final String NO_REFRESH = "Pas de rafraîchissement en cours   ";

    /**
     * RefreshText
     */
    private static final String REFRESH = "Rafraîchissement en cours : ";

    /**
     * Constructeur
     */
    public RefreshView( Composite parent )
    {
        super( parent, SWT.BORDER );
        this.setLayout( new RowLayout( SWT.HORIZONTAL ) );
    }

    /**
     * @see com.tvshows.front.ui.FrontUI#initComposites()
     */
    @Override
    public void initComposites()
    {
        this.setLayout( new RowLayout() );

        progressLabel = new FrontLabel( this, SWT.NONE );
        progressLabel.setSize( 100, 30 );
        progressLabel.setLayoutData( new RowData( 275, 15 ) );
        progressLabel.setText( NO_REFRESH );

        progressBar = new ProgressBar( this, SWT.SMOOTH );
        progressBar.setLayoutData( new RowData( 260, 15 ) );
        progressBar.setSelection( 0 );
        progressBar.setVisible( false );

        this.layout();
        ObserverBdd.getObserverBdd().setRefreshView( this );
    }

    /**
     * Updates the progress bar
     * 
     * @param update the increment update
     */
    @Override
    public void updateProgress( int update )
    {
        progressBar.setSelection( progressBar.getSelection() + update );
    }

    /**
     * @see com.tvshows.front.ui.refresh.RefreshUI#activateProgressBar(boolean)
     */
    @Override
    public void activateProgressBar( boolean activate )
    {
        this.progressBar.setVisible( activate );
        if ( !activate )
        {
            progressLabel.setText( NO_REFRESH );
            formerProgress = 0;
            this.progressBar.setSelection( 0 );
            this.layout();
        }
    }

    /**
     * @see com.tvshows.front.ui.refresh.RefreshUI#updateProgressLabel(double)
     */
    @Override
    public void updateProgressLabel( double progress )
    {
        NumberFormat formatter = new DecimalFormat( "#0.00" );
        progressLabel.setText( REFRESH + formatter.format( progress + formerProgress ) + " %" );

        formerProgress += progress;
    }

    /**
     * @see com.tvshows.front.ui.refresh.RefreshUI#isSelection()
     */
    @Override
    public boolean isSelection()
    {
        return isSelection;
    }

    /**
     * @see com.tvshows.front.ui.refresh.RefreshUI#setSelection(int)
     */
    @Override
    public void setSelection( int progress )
    {
        this.progressBar.setMaximum( Integer.parseInt( String.valueOf( progress ) ) );
        this.isSelection = true;
    }

    /**
     * @see com.tvshows.front.ui.FrontUI#initFormData()
     */
    @Override
    public void initFormData()
    {
        // UNUSED

    }

}
